<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blogs/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blogs/images/favicon.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/blogs/images/favicon.ico">
  <link rel="mask-icon" href="/blogs/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blogs/css/main.css">


<link rel="stylesheet" href="/blogs/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('https://tretea.gitee.io').hostname,
    root: '/blogs/',
    scheme: 'Gemini',
    version: '7.6.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    comments: {"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}
  };
</script>

  <meta name="description" content="js面向对象的学习">
<meta property="og:type" content="article">
<meta property="og:title" content="js中的面向对象编程">
<meta property="og:url" content="https://tretea.gitee.io/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/index.html">
<meta property="og:site_name" content="小叶子的个人博客">
<meta property="og:description" content="js面向对象的学习">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tretea.gitee.io/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/prototype.jpg">
<meta property="og:image" content="https://tretea.gitee.io/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/%E5%8E%9F%E5%9E%8B%E9%93%BE.jpg">
<meta property="og:image" content="https://tretea.gitee.io/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0.jpg">
<meta property="article:published_time" content="2020-05-11T05:06:07.000Z">
<meta property="article:modified_time" content="2020-05-18T14:44:29.128Z">
<meta property="article:author" content="小叶子">
<meta property="article:tag" content="原型、原型链">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tretea.gitee.io/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/prototype.jpg">

<link rel="canonical" href="https://tretea.gitee.io/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>js中的面向对象编程 | 小叶子的个人博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/blogs/atom.xml" title="小叶子的个人博客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
    <a href="https://github.com/tretea" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/blogs/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小叶子的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blogs/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blogs/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blogs/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blogs/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blogs/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://tretea.gitee.io/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blogs/images/%E5%B0%8F%E5%8F%B6%E5%AD%90.jpg">
      <meta itemprop="name" content="小叶子">
      <meta itemprop="description" content="也许我会走的很慢，但我不会停下来">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小叶子的个人博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          js中的面向对象编程
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-05-11 13:06:07" itemprop="dateCreated datePublished" datetime="2020-05-11T13:06:07+08:00">2020-05-11</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-18 22:44:29" itemprop="dateModified" datetime="2020-05-18T22:44:29+08:00">2020-05-18</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blogs/categories/JavaScript/" itemprop="url" rel="index">
                    <span itemprop="name">JavaScript</span>
                  </a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/#comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.4k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>js面向对象的学习</p>
<a id="more"></a>
<h1 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h1><p>本文参考<br><a href="https://www.cnblogs.com/shuiyi/p/5305435.html" target="_blank" rel="noopener">https://www.cnblogs.com/shuiyi/p/5305435.html</a><br><a href="https://www.zhihu.com/question/34183746" target="_blank" rel="noopener">https://www.zhihu.com/question/34183746</a></p>
<h1 id="一、js中的面向对象"><a href="#一、js中的面向对象" class="headerlink" title="一、js中的面向对象"></a>一、js中的面向对象</h1><p>js不像python,java等面向对象语言有<strong>class</strong>的概念,它的理念是<strong>万物皆对象</strong>,任何数据都是<strong>对象</strong>。js抽象出了<strong>原型</strong>来实现<strong>面向对象编程</strong>。</p>
<h1 id="二、js对象的声明"><a href="#二、js对象的声明" class="headerlink" title="二、js对象的声明"></a>二、js对象的声明</h1><h2 id="1-字面量"><a href="#1-字面量" class="headerlink" title="1.字面量"></a>1.字面量</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="2-new一个对象"><a href="#2-new一个对象" class="headerlink" title="2.new一个对象"></a>2.new一个对象</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = <span class="keyword">new</span> object</span><br></pre></td></tr></table></figure>

<h2 id="3-构造函数创建"><a href="#3-构造函数创建" class="headerlink" title="3.构造函数创建"></a>3.构造函数创建</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Obj</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h1 id="三、对象"><a href="#三、对象" class="headerlink" title="三、对象"></a>三、对象</h1><h2 id="生成学生对象"><a href="#生成学生对象" class="headerlink" title="生成学生对象"></a>生成学生对象</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> stu1=&#123;</span><br><span class="line">    name:<span class="string">'小明'</span>,</span><br><span class="line">    sex:<span class="string">'男'</span>,</span><br><span class="line">    learning:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'学习中...'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> stu2=&#123;</span><br><span class="line">    name:<span class="string">'小红'</span>,</span><br><span class="line">    sex:<span class="string">'女'</span>,</span><br><span class="line">    learning:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'学习中...'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> stu3=&#123;</span><br><span class="line">    name:<span class="string">'小刚'</span>,</span><br><span class="line">    sex:<span class="string">'男'</span>,</span><br><span class="line">    learning:<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'学习中...'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(stu1,stu2,stu3)</span><br><span class="line"></span><br><span class="line">生成了三个学生对象,有name,sex属性和learning公共方法</span><br><span class="line"></span><br><span class="line">可以看到学生对象有一个learning公共属性,每次生成一个对象都要创建一次</span><br></pre></td></tr></table></figure>
<h2 id="通过new-一个构造器生成"><a href="#通过new-一个构造器生成" class="headerlink" title="通过new 一个构造器生成"></a>通过new 一个构造器生成</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> Student=<span class="function"><span class="keyword">function</span>(<span class="params">name,sex</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name=name</span><br><span class="line">    <span class="keyword">this</span>.sex=sex</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> stu1=<span class="keyword">new</span> Student(<span class="string">'小明'</span>,<span class="string">'男'</span>)</span><br><span class="line"><span class="keyword">var</span> stu2=<span class="keyword">new</span> Student(<span class="string">'小红'</span>,<span class="string">'女'</span>)</span><br><span class="line"><span class="keyword">var</span> stu3=<span class="keyword">new</span> Student(<span class="string">'小刚'</span>,<span class="string">'男'</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(stu1,stu2,stu3)</span><br></pre></td></tr></table></figure>
<h2 id="构造器"><a href="#构造器" class="headerlink" title="构造器"></a>构造器</h2><p>js中没有类,只有函数式声明替代类</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Person</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name=name</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> XiaoMing=<span class="keyword">new</span> Person(XiaoMing)</span><br><span class="line"></span><br><span class="line">XiaoMing的构造器就是Person()</span><br></pre></td></tr></table></figure>
<ul>
<li>构造器用于批量化生产对象</li>
<li>首字母大写默认为一个构造器</li>
<li>js中的对象有<strong>函数对象(构造器)</strong>和<strong>普通对象</strong>两种</li>
<li><strong>构造器</strong>的原型是<strong>Function</strong>,<strong>Function</strong>的原型也是<strong>Function</strong></li>
<li><strong>普通对象</strong>的原型是<strong>Object</strong></li>
</ul>
<h1 id="四、prototype与proto"><a href="#四、prototype与proto" class="headerlink" title="四、prototype与proto"></a>四、prototype与<strong>proto</strong></h1><h2 id="1-prototype-显式原型"><a href="#1-prototype-显式原型" class="headerlink" title="1.prototype(显式原型)"></a>1.prototype(显式原型)</h2><p>js中所有数据都是<strong>对象</strong>,所以函数<strong>Function</strong>也是<strong>对象</strong>,它是一个<strong>特殊</strong>对象,它有一个普通对象没有的属性<strong>prototype</strong>,其中包含自身<strong>共享被继承</strong>的所有属性与方法,它本身有<strong>constructor属性与<strong>proto</strong></strong> </p>
<h2 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h2><ul>
<li><strong>prototype也是一个普通对象,所以也有<strong>proto</strong>属性,<strong>proto</strong>指向的是Object的prototype</strong>  </li>
<li><strong>Object,String,Array这些都是构造器,所以它们的<strong>proto</strong>==Function.prototype</strong><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建Fun构造器</span></span><br><span class="line"><span class="keyword">var</span> Fun=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">f</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">'这是f函数'</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 以Fun为构造器,创建实例obj</span></span><br><span class="line"><span class="keyword">var</span> obj=<span class="keyword">new</span> Fun();</span><br><span class="line"><span class="comment">// 普通对象的原型指向Object的原型对象</span></span><br><span class="line">obj.__proto__===<span class="built_in">Object</span>.prototype <span class="comment">//true</span></span><br><span class="line">Fun.prototype.__proto__===<span class="built_in">Object</span>.prototype <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 构造器的原型指向Function的原型对象</span></span><br><span class="line"><span class="built_in">Function</span>.__proto__===<span class="built_in">Function</span>.prototype <span class="comment">//true</span></span><br><span class="line"><span class="built_in">Object</span>.__proto__===<span class="built_in">Function</span>.prototype <span class="comment">//true</span></span><br><span class="line"><span class="built_in">String</span>.__proto__===<span class="built_in">Function</span>.prototype <span class="comment">//true</span></span><br><span class="line"><span class="built_in">Array</span>.__proto__===<span class="built_in">Function</span>.prototype <span class="comment">//true</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<p><img src="/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/prototype.jpg" alt="prototype"></p>
<h2 id="2-proto隐式原型"><a href="#2-proto隐式原型" class="headerlink" title="2.proto隐式原型"></a>2.<strong>proto</strong>隐式原型</h2><p>在js中,<strong>每一个对象</strong>都有一个<strong><strong>proto</strong>(隐式原型)属性</strong>,<strong><strong>proto</strong></strong>指向的是<strong>构造函数的prototype(原型对象)</strong>,因为构造函数是一个函数对象,所以有prototype属性,包含<strong>构造函数被继承的公共部分</strong>,对象就可以调用它<strong><strong>proto</strong></strong>中的所有属性与方法,实现继承<br><img src="/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/%E5%8E%9F%E5%9E%8B%E9%93%BE.jpg" alt="原型链"></p>
<h2 id="3-constructor-构造函数"><a href="#3-constructor-构造函数" class="headerlink" title="3.constructor(构造函数)"></a>3.constructor(构造函数)</h2><p>对象.constructor=构造函数=构造函数.prototype.constructor<br>构造函数.constructor=Function</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> F=<span class="function"><span class="keyword">function</span>(<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.a=a;</span><br><span class="line">    <span class="keyword">this</span>.b=b;</span><br><span class="line">    <span class="keyword">this</span>.add=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.a+<span class="keyword">this</span>.b;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> obj1=<span class="keyword">new</span> F();</span><br><span class="line"><span class="built_in">console</span>.log(obj1.constructor)</span><br><span class="line"><span class="built_in">console</span>.log(F.constructor)</span><br><span class="line"></span><br><span class="line">结果:</span><br><span class="line">   (a,b)&#123;</span><br><span class="line">    <span class="keyword">this</span>.a=a;</span><br><span class="line">    <span class="keyword">this</span>.b=b;</span><br><span class="line">    <span class="keyword">this</span>.add=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>.a+<span class="keyword">this</span>.b;</span><br><span class="line">    &#125;; <span class="comment">//obj1的构造函数F()</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">Function</span>() &#123; [native code] &#125;  <span class="comment">//F的构造函数Function()</span></span><br></pre></td></tr></table></figure>
<p><img src="/blogs/2020/05/11/js%E4%B8%AD%E7%9A%84%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0.jpg" alt="构造函数"></p>

    </div>

    
    
    

      <div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
  
</div>
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blogs/tags/%E5%8E%9F%E5%9E%8B%E3%80%81%E5%8E%9F%E5%9E%8B%E9%93%BE/" rel="tag"># 原型、原型链</a>
              <a href="/blogs/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blogs/2020/05/10/less/" rel="prev" title="less">
      <i class="fa fa-chevron-left"></i> less
    </a></div>
      <div class="post-nav-item">
    <a href="/blogs/2020/05/15/call-applay-bind%E7%9A%84%E7%94%A8%E6%B3%95/" rel="next" title="call,applay,bind的用法">
      call,applay,bind的用法 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#引用"><span class="nav-number">1.</span> <span class="nav-text">引用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#一、js中的面向对象"><span class="nav-number">2.</span> <span class="nav-text">一、js中的面向对象</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#二、js对象的声明"><span class="nav-number">3.</span> <span class="nav-text">二、js对象的声明</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-字面量"><span class="nav-number">3.1.</span> <span class="nav-text">1.字面量</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-new一个对象"><span class="nav-number">3.2.</span> <span class="nav-text">2.new一个对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-构造函数创建"><span class="nav-number">3.3.</span> <span class="nav-text">3.构造函数创建</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#三、对象"><span class="nav-number">4.</span> <span class="nav-text">三、对象</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#生成学生对象"><span class="nav-number">4.1.</span> <span class="nav-text">生成学生对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#通过new-一个构造器生成"><span class="nav-number">4.2.</span> <span class="nav-text">通过new 一个构造器生成</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#构造器"><span class="nav-number">4.3.</span> <span class="nav-text">构造器</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#四、prototype与proto"><span class="nav-number">5.</span> <span class="nav-text">四、prototype与proto</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-prototype-显式原型"><span class="nav-number">5.1.</span> <span class="nav-text">1.prototype(显式原型)</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#注意"><span class="nav-number">5.2.</span> <span class="nav-text">注意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-proto隐式原型"><span class="nav-number">5.3.</span> <span class="nav-text">2.proto隐式原型</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-constructor-构造函数"><span class="nav-number">5.4.</span> <span class="nav-text">3.constructor(构造函数)</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="小叶子"
      src="/blogs/images/%E5%B0%8F%E5%8F%B6%E5%AD%90.jpg">
  <p class="site-author-name" itemprop="name">小叶子</p>
  <div class="site-description" itemprop="description">也许我会走的很慢，但我不会停下来</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blogs/archives/">
        
          <span class="site-state-item-count">57</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blogs/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blogs/tags/">
          
        <span class="site-state-item-count">48</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tretea" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tretea" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/tretea" title="gitee → https:&#x2F;&#x2F;gitee.com&#x2F;tretea" rel="noopener" target="_blank"><i class="fa fa-fw fa-gitee"></i>gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2433469529@qq.com" title="E-Mail → mailto:2433469529@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://space.bilibili.com/88409744" title="bilibili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;88409744" rel="noopener" target="_blank"><i class="fa fa-fw fa-bilibili"></i>bilibili</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 2019 – 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-孙智超"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">孙智超</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">84k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">2:20</span>
</div>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/blogs/lib/anime.min.js"></script>
  <script src="/blogs/lib/velocity/velocity.min.js"></script>
  <script src="/blogs/lib/velocity/velocity.ui.min.js"></script>

<script src="/blogs/js/utils.js"></script>

<script src="/blogs/js/motion.js"></script>


<script src="/blogs/js/schemes/pisces.js"></script>


<script src="/blogs/js/next-boot.js"></script>




  




  
<script src="/blogs/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el: '#valine-comments',
      verify: false,
      notify: true,
      appId: 'joFkPTxLK8qvkQ7bk3l11d9w-gzGzoHsz',
      appKey: 'FoRjyOtjoXMgYfOHTlzmL1iQ',
      placeholder: "请输入您的评论",
      avatar: '小叶子',
      meta: guest,
      pageSize: '10' || 10,
      visitor: false,
      lang: '' || 'zh-cn',
      path: location.pathname,
      recordIP: false,
      serverURLs: ''
    });
  }, window.Valine);
});
</script>

<script src="/blogs/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"model":{"jsonPath":"/blogs/live2dw/assets/wanko.model.json"},"display":{"position":"right","width":300,"height":300},"mobile":{"show":false},"log":false});</script></body>
</html>
